package com.ruoyi.service.impl;

import com.jnxx.task.service.impl.OkrStaffServiceImpl;
import com.ruoyi.domain.TCopyRecord;
import com.ruoyi.mapper.TCopyRecordMapper;
import com.ruoyi.service.ITCopyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 抄送记录Service业务层处理
 * 
 * @author mzr
 * @date 2023-12-13
 */
@Service
public class TCopyRecordServiceImpl implements ITCopyRecordService 
{
    @Autowired
    private TCopyRecordMapper tCopyRecordMapper;

    @Autowired
    private OkrStaffServiceImpl staffService;

    /**
     * 查询抄送记录
     * 
     * @param id 抄送记录主键
     * @return 抄送记录
     */
    @Override
    public TCopyRecord selectTCopyRecordById(Long id)
    {
        return tCopyRecordMapper.selectTCopyRecordById(id);
    }

    /**
     * 查询抄送记录列表
     * 
     * @param tCopyRecord 抄送记录
     * @return 抄送记录
     */
    @Override
    public List<TCopyRecord> selectTCopyRecordList(TCopyRecord tCopyRecord)
    {
        List<TCopyRecord> tCopyRecords = tCopyRecordMapper.selectTCopyRecordList(tCopyRecord);
        if (tCopyRecords.size() >0){
            for (TCopyRecord copyRecord : tCopyRecords) {
                String makeUser = copyRecord.getMakeUser();
                String[] split = makeUser.split(",");
                List<Long> longList = Arrays.stream(split)
                        .map(Long::parseLong)
                        .collect(Collectors.toList());
                if (split.length > 0){
                    String[] name = staffService.findNameByIds(longList);
                    String userName = Arrays.stream(name).collect(Collectors.joining("，"));
                    copyRecord.setUserName(userName);
                }
            }
        }

        return tCopyRecords;
    }

    /**
     * 新增抄送记录
     * 
     * @param tCopyRecord 抄送记录
     * @return 结果
     */
    @Override
    public int insertTCopyRecord(TCopyRecord tCopyRecord)
    {
        return tCopyRecordMapper.insertTCopyRecord(tCopyRecord);
    }

    /**
     * 修改抄送记录
     * 
     * @param tCopyRecord 抄送记录
     * @return 结果
     */
    @Override
    public int updateTCopyRecord(TCopyRecord tCopyRecord)
    {
        return tCopyRecordMapper.updateTCopyRecord(tCopyRecord);
    }

    /**
     * 批量删除抄送记录
     * 
     * @param ids 需要删除的抄送记录主键
     * @return 结果
     */
    @Override
    public int deleteTCopyRecordByIds(Long[] ids)
    {
        return tCopyRecordMapper.deleteTCopyRecordByIds(ids);
    }

    /**
     * 删除抄送记录信息
     * 
     * @param id 抄送记录主键
     * @return 结果
     */
    @Override
    public int deleteTCopyRecordById(Long id)
    {
        return tCopyRecordMapper.deleteTCopyRecordById(id);
    }
}
